﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;

namespace DevEx
{
    public class CDonVi
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }
                
        public IQueryable<tblDonVi> SelectAllRecord()
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblDonVis
                    orderby p.Cuoi, p.DonVi
                    select p;
            return q;
        }

        public IQueryable<tblDonVi> SelectAllRecordByParentID(int parentID)
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblDonVis
                    where p.ID_Parent == parentID
                    orderby p.Cuoi, p.DonVi
                    select p;
            return q;
        }

        public IQueryable<tblDonVi> SelectAllRecordByCuoi(Boolean _cuoi)
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblDonVis
                    where p.Cuoi == _cuoi
                    orderby p.DonVi
                    select p;
            return q;
        }

        public tblDonVi SelectRecordByID(int ID_DonVi)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDonVis where p.ID_DonVi == ID_DonVi select p).SingleOrDefault();
            return q;
        }

        public tblDonVi SelectRecordByName(String DonVi)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDonVis where p.DonVi.ToUpper() == DonVi.ToUpper() select p).SingleOrDefault();
            return q;
        }

        //Insert
        public void Insert(tblDonVi obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblDonVis.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();

            var DonVi = dc.tblDonVis.SingleOrDefault(p=>p.ID_DonVi == ID);
            dc.tblDonVis.DeleteOnSubmit(DonVi);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblDonVi obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblDonVis.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
    }
}
